clear all
set more off
capture log close
log using "$output/figureA5.log", replace

version 16	

use "$data/ctctaxsim_mar1kid1.dta", clear
append using "$data/ctctaxsim_mar1kid2.dta"
append using "$data/ctctaxsim_mar1kid3.dta"
tab depx

sum fiitax earnings fedtaxlessctc v25 totctc
replace earnings=earnings*1000
replace ctc=ctc*1000
replace v25=v25*1000
replace totctc=totctc*1000
sum fiitax earnings fedtaxlessctc v25 totctc

gen avgtaxrate=fiitax/earnings
gen avgtaxrate2021=fedtaxlessctc/earnings	/* Don't include new CTC */

gen avgtaxeitc=-v25/earnings
gen avgtaxctc=-totctc/earnings
gen avgtaxctc2021=0

gen avgtaxeitcctc=avgtaxeitc+avgtaxctc

gen avginctax=avgtaxrate- avgtaxeitcctc

tab year avginctax if earnings==40000 & depx==1
replace avginctax=avginctax*.04/.037875 if year==2017
tab year avginctax if earnings==40000 & depx==1

foreach var in avgtaxeitc avgtaxctc avgtaxctc2021 avginctax{
	replace `var'=-`var'
}

foreach var in avgtaxeitc avgtaxctc avgtaxctc2021 avginctax{
	replace `var'=`var'+1
}

replace avgtaxrate=-avgtaxrate+1
replace avgtaxrate2021=-avgtaxrate2021+1

bysort earnings depx: egen minavgtax=min(avgtaxrate)
gen difavgtax2=avgtaxrate2021-minavgtax
sum difavgtax2

bysort earnings depx: egen maxavgtax=max(avgtaxrate)
gen difavgtax=avgtaxrate2021-maxavgtax
sum difavgtax

gen difavgtax3=minavgtax-maxavgtax

sort earnings depx year 
order year earnings depx *avgtax* dif*
drop if year==2017
replace difavgtax3=-difavgtax3

twoway line difavgtax2 earnings if year==2018 & depx==1, lp(-) pstyle(p1) || line difavgtax2 earnings if year==2018 & depx==2, lp(_) pstyle(p2) || line difavgtax2 earnings if year==2018 & depx==3, pstyle(p1) graphregion(color(white)) bgcolor(white) leg(order(1 "1 Kid" 2 "2 Kids" 3 "3 Kids") c(1) pos(5) ring(0)) ytitle("Change in Return to Work") xtitle("Earnings" "{bf:Panel A: 2021 vs 2020}") 
graph save "$output/avgtax2.gph", replace


twoway line difavgtax earnings if year==2018 & depx==1, lp(-) pstyle(p1) || line difavgtax earnings if year==2018 & depx==2, lp(_) pstyle(p2) || line difavgtax earnings if year==2018 & depx==3, pstyle(p1) graphregion(color(white)) bgcolor(white) leg(order(1 "1 Kid" 2 "2 Kids" 3 "3 Kids") c(1) pos(5) ring(0)) ytitle("Change in Return to Work") xtitle("Earnings" "{bf:Panel B: 2021 vs 2017}")  
graph save "$output/avgtax.gph", replace


twoway line difavgtax3 earnings if year==2018 & depx==1, lp(-) pstyle(p1) || line difavgtax3 earnings if year==2018 & depx==2, lp(_) pstyle(p2) || line difavgtax3 earnings if year==2018 & depx==3, pstyle(p1) graphregion(color(white)) bgcolor(white) leg(order(1 "1 Kid" 2 "2 Kids" 3 "3 Kids") c(1) pos(2) ring(0)) ytitle("Change in Return to Work") xtitle("Earnings" "{bf:Panel C: 2020 vs 2017}")  
graph save "$output/avgtax3.gph", replace


graph combine "$output/avgtax2.gph" "$output/avgtax.gph" "$output/avgtax3.gph",graphregion(color(white)) 
graph export "$output/figureA5.png", replace

erase "$output/avgtax2.gph" 
erase "$output/avgtax.gph" 
erase"$output/avgtax3.gph"

log close